package com.x.erp.dao;

import com.x.erp.model.CarValuation;
import com.x.framework.Base;
import com.x.framework.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 车辆估值DAO
 */
@Repository
public class CarValuationDao extends BaseDao {

	private final static String selectCarValuationByCarIdSql = "SELECT " +
			"ecv.* " +
			"FROM " +
			"ERP_CAR_VALUATION ecv " +
			"WHERE ecv.CAR_ID = ? ";

	/**
	 * 查询车辆估值信息
	 * @param carValuation
	 * @return
	 * @throws Exception
	 */
	public List<CarValuation> selectCarValuationByCarId(CarValuation carValuation) throws Exception {
		StringBuilder sql = new StringBuilder(selectCarValuationByCarIdSql);
		List<Object> params = new ArrayList<Object>();
		params.add(carValuation.getCarId());
		return this.queryForList(sql.toString(), CarValuation.class, params);
	}

	private final static String insertCarValuationListSql = "INSERT INTO ERP_CAR_VALUATION VALUES ";

	/**
	 * 批量添加车辆估值信息
	 * @param carId
	 * @param carValuations
	 * @return
	 * @throws Exception
	 */
	public void insertCarValuationList(String carId, List<CarValuation> carValuations) throws Exception {
		StringBuilder sql = new StringBuilder(insertCarValuationListSql);
		List<Object> params = new ArrayList<>();
		if (carValuations == null || carValuations.size() ==0){
			return;
		} else {
			for (int i = 0;i < carValuations.size();i++){
				sql.append("(?,?,?,?,?,?,?,?,?,?,?,?,?,?),");
				params.add(Base.getUid(32));
				params.add(carValuations.get(i).getValuationDefaultCarCondition());
				params.add(carValuations.get(i).getValuationCondition());
				params.add(carValuations.get(i).getValuationDealerLowBuyPrice());
				params.add(carValuations.get(i).getValuationDealerBuyPrice());
				params.add(carValuations.get(i).getValuationIndividualLowSoldPrice());
				params.add(carValuations.get(i).getValuationIndividualPrice());
				params.add(carValuations.get(i).getValuationDealerLowSoldPrice());
				params.add(carValuations.get(i).getValuationDealerPrice());
				params.add(carValuations.get(i).getValuationDealerHighSoldPrice());
				params.add(this.getDate(0));
				params.add(carValuations.get(i).getValuationDetailReportUrl());
				params.add(carValuations.get(i).getValuationFailReason());
				params.add(carId);
			}
			this.execute(sql.toString().substring(0,sql.length() - 1), params);
		}
	}

	private final static String deleteCarValuationListSql = "DELETE FROM ERP_CAR_VALUATION WHERE CAR_ID = ? ";
	/**
	 * 批量删除车辆估值信息
	 * @param carId
	 * @return
	 * @throws Exception
	 */
	public void deleteCarValuationList(String carId) throws Exception {
		StringBuilder sql = new StringBuilder(deleteCarValuationListSql);
		List<Object> params = new ArrayList<>();
		params.add(carId);
		this.execute(sql.toString(), params);
	}
}
